package com.qianyitian.hope2.analyzer.controller;

import com.qianyitian.hope2.analyzer.util.JwtTokenUtil;
import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class AuthController {

    Map<String, String> db = new HashMap<>();

    Map<String, String> cache = new HashMap<>();

    String tokenSecret="safjweurqianyitian";


    public AuthController() {
        db.put("afwor23u8sdfsjf234sfs4", "238726548940038459764823498325");
        db.put("29sofmxclvjuwerjlfdsfysf", "08429848763472850384503759872947");
    }


    @PostMapping("/token/apply")
    public String apply(@RequestParam String accessKey, String accessSecret) {

        if (!db.get(accessKey).equals(accessSecret)) {
            return "error accessKey and accessSecret";
        }

        String userId = accessKey;
        String role = "api-role";

        // 创建token
        String token = JwtTokenUtil.createJWT(userId, userId, role, tokenSecret);
        //这里有bug
        cache.put(token, accessSecret);

        return token;
    }


    @PostMapping("/token/verify")
    public String verify(@RequestParam String token) {
        String ok= JwtTokenUtil.parseJWT(token, tokenSecret);
        return ok;
    }

}
